/**
    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) 2010  Sean Beecroft

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

 * @version 1.10 2010-01-01
 * @author Sean Beecroft
 */
package yawiki.server.filters;
import yawiki.server.symbols.Section;
import yawiki.server.util.StringUtility;

import java.net.*;

public class Filter 
{
	
	public static boolean filterLink(String link)
	{

			String[] generalFilterCodes = {"Wikipedia", "Wikisaurus", "Wiktionary", "User", "Category", "Special", "Template"};
		
			String[] langCodes = {
		
			"Cs","Ca", "Cd","Bg","Bs", "Be", "Zh-min-nan", "Als", "Ar", "Al", "As", "Af", "Bm", "Fa",
			"Fr", "Da", "De", "Gd", "Ga", "Et", "Es", "Ei", "Eo", "Eu", "Ko", "He", "Ke", "Kn", "Ku",
			"Lv", "Ln", "Hu", "Mk", "Ms", "Nl", "No", "Nn", "Pt", "Pl", "Qu", "Scn", "Sk", "Sl", "Fi", "Sv", 
			"Ta", "Tt", "Th", "Vi", "Tr", "Uk", "Zh", "Ja", "Ru","Sr", "Hr", "Id", "It", "Lt",
			"An", "Ast", "Az", "Bn", "Br", "Cv", "Cy", "Dv", "El", "Fy", "Fur", "Gl", "Gu", "Hy",
			"Hi", "Hr", "Io", "Ilo", "Id", "Ia", "Os", "Is", "It", "Ka", "Kw", "Ht", "La", "Lb", 
			"Lt", "Li", "Ml", "Mo", "Na", "Oc", "Ug", "Pam", "Ps", "Km", "Nds", "Ro", "Ru", "Se", 
			"Sa", "Sq", "Simple", "Sr", "Sh", "Tk", "Udm", "Uz", "Tg", "Tl", "It", "Sr", "Ro", "Sw", "Zh-yue"
			};

			String tok = ":";
			for( int i = 0; i < langCodes.length; i++  ){
				try { 
					if( StringUtility.indexOfIgnoreCase(link, langCodes[i] + tok) != -1 ) {
						return false;
					} else if( StringUtility.indexOfIgnoreCase(link, URLEncoder.encode( langCodes[i] + tok, "UTF-8")) != -1 ) {
						return false;
					}
				} catch(Exception ex){}
				
			}
			for( int i = 0; i < generalFilterCodes.length; i++  ){
				try { 
					if( StringUtility.indexOfIgnoreCase(link, generalFilterCodes[i] + tok) != -1 ) {
						return false;
					} else if( StringUtility.indexOfIgnoreCase(link, URLEncoder.encode(generalFilterCodes[i] + tok, "UTF-8")) != -1 ) {
						return false;
					}
				} catch(Exception ex) { } 
			}

			return true;

	}

	public static boolean filterElement(Section wikiElement) {

        if (wikiElement.getTitle() == null) { 
            return false;
        }

        String s = wikiElement.getTitle();

        //System.out.println(s);
        
        String text = wikiElement.getText();
        
        if( text == null ){
        	return false;
        }
        text = text.trim();
        if( text.equals("")){
        	return false;
        }
   
        
        if (StringUtility.indexOfIgnoreCase(wikiElement.getTitle(), "External links") != -1) { 
            return false;
        } else if (StringUtility.indexOfIgnoreCase(wikiElement.getTitle(), "External link") != -1) { 
            return false;
        } else if (StringUtility.indexOfIgnoreCase(wikiElement.getTitle(), "References") != -1) { 
            return false;
        }

        return true;
    }
}